<?php
namespace GRH\Controller;

use GRH\Model\Site;
use GRH\Form\SiteForm;
use GRH\Model\Table\SiteTable;

class SiteController extends BaseController
{
    /**
     * Lister les sites
     * 
     * @return type
     */
    public function indexAction()
    {
        $siteTable = $this->getService('GRH\Model\Table\SiteTable');
        $siteList = $siteTable->getAll();
        $newSite = new SiteForm('new-site');
        
        return array(
            'siteList' => $siteList,
            'newSite' => $newSite,
        );
    }
    
    public function addAction()
    {
        $request = $this->getRequest();
        if($request->isPost())
        {
            // Ajout dans la base de données
            $site = new Site();
            $site->exchangeArray($request->getPost());
            $this->getService('GRH\Model\Table\SiteTable')
                    ->add($site);
        }
        
        return $this->redirect()
                ->toRoute('GRH/default', array(
                    'controller' => 'site',
                    'action' => 'index',
                ));
    }
    
    public function editAction()
    {
        $id = (int)$this->params()->fromRoute('id', -1);
        if($id < 0)
            return $this->redirect()->toRoute('GRH/default', array('controller' => 'site', 'action' => 'errorsite', 'id' => $id));
        try{
            $site = $this->getService('GRH\Model\Table\SiteTable')
                ->get($id);
        }catch(\Exception $ex){
            return $this->redirect()->toRoute('GRH/default', array('controller' => 'site', 'action' => 'errorsite', 'id' => $id));
        }
        $form = new SiteForm();
        $form->bind($site);
        
        $request = $this->getRequest();
        if($request->isPost())
        {
            $site->exchangeArray($request->getPost());
            $this->getService('GRH\Model\Table\SiteTable')
                ->save($site);
            return $this->redirect()->toRoute('GRH/default', array('controller' => 'site', 'action' => 'success'));
        }
        $this->layout('layout/ajax');
        
        return array(
            'id' => $id,
            'form' => $form,
        );
    }
    
    public function deleteAction()
    {
        $id = (int)$this->params()->fromRoute('id', -1);
        if($id < 0)
            return $this->redirect()->toRoute('GRH/default', array('controller' => 'site', 'action' => 'errorsite', 'id' => $id));
        $request = $this->getRequest();
        if($request->isPost())
        {
            // Effacer le site
            $site_id = (int)$request->getPost('site_id');
            var_dump($site_id);
            $this->getService('GRH\Model\Table\SiteTable')
                    ->erase($site_id);

            return $this->redirect()->toRoute('GRH/default', array('controller' => 'site', 'action' => 'success'));
        }
        // Affichage simple
        $this->layout('layout/ajax');
        
        return array(
            'id' => $id,
            'site' => $this->getService('GRH\Model\Table\SiteTable')
                        ->get($id),
        );
    }
    
    public function errorsiteAction()
    {
        $id = (int)  $this->params()->fromRoute('id', 0);
        $this->layout('layout/ajax');
        
        return array(
            'id' => $id,
        );
    }
    
    public function successAction()
    {
        $this->layout('layout/ajax');
        
        return array();
    }
}